//
//  AppDelegate+RootViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/16.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "AppDelegate+RootViewController.h"
#import "ZFTabbarViewController.h"
#import "TradePswViewController.h"
#import "ZFAppleViewController.h"
#import "ZFHeader.h"
#import "ZFLoginViewController.h"
//#import "AppDelegate+AppBasicIformation.h"
#import "upLoadAppApiRequest.h"
#import "LPSwipeNavigationController.h"
#import "strategyViewController.h"
#import "secondViewController.h"
#import "thirdViewController.h"
#import "fourthViewController.h"

@implementation AppDelegate (RootViewController)

- (void)enterApp
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.isFirstEnterApp = YES;
        //第一次启动
        //引导页
        UIViewController *emptyView = [[ UIViewController alloc ]init ];
        self.window.rootViewController = emptyView;
        [self createLoadingScrollView];
    }
    else {
        
        [self setTabbarController];
        [ZFHomeHttpRequest requestAppSwitchComplete:^(NSDictionary *dict) {
            
            if ([dict[@"code"] integerValue] == 1) {
                
                NSString *flag = [NSString stringWithFormat:@"%@",dict[@"data"][@"switch_flag"]];
                self.app_flag = [flag integerValue];
                if (self.app_flag == 1) {
                    
                    [self setTabbarController1];
                }
            }
        } failureBlock:^(NSInteger failSign) {
            
        }];

    }
    

}
#pragma mark - 引导页
- (void)createLoadingScrollView
{   //引导页
    UIScrollView *sc = [[UIScrollView alloc]initWithFrame:self.window.bounds];
    sc.pagingEnabled = YES;
    sc.delegate = self;
    sc.showsHorizontalScrollIndicator = NO;
    sc.showsVerticalScrollIndicator = NO;
    sc.bounces = NO;
    [self.window.rootViewController.view addSubview:sc];
    
    NSArray *arr = @[@"page01.png",@"page02.png",@"page03.png"];
    for (NSInteger i = 0; i<arr.count; i++)
    {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, self.window.frame.size.height)];
        img.image = [UIImage imageNamed:arr[i]];
        [sc addSubview:img];
        img.userInteractionEnabled = YES;
        if (i == arr.count - 1)
        {
            UITapGestureRecognizer *tapIamge = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toTap:)];
            [img addGestureRecognizer:tapIamge];
        }
    }
    sc.contentSize = CGSizeMake(SCREEN_WIDTH*arr.count, self.window.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > SCREEN_WIDTH *3+30)
    {
        [self setTabbarController];
        [ZFHomeHttpRequest requestAppSwitchComplete:^(NSDictionary *dict) {
            
            if ([dict[@"code"] integerValue] == 1) {
                
                NSString *flag = [NSString stringWithFormat:@"%@",dict[@"data"][@"switch_flag"]];
                self.app_flag = [flag integerValue];
                if (self.app_flag == 1) {
                    
                    [self setTabbarController1];
                }
            }
        } failureBlock:^(NSInteger failSign) {
            
        }];

    }
}
- (void)toTap:(UITapGestureRecognizer *)tap{
     [self setTabbarController];
}

/**
 *  根视图
 */
- (void)setRootViewController
{
    
}
/**
 *  tabbar实例
 */
- (void)setTabbarController
{
    [self setBarAppearance];

    ZFHomeViewController *homeVC = [ZFHomeViewController new];
    homeVC.tabBarItem.title = @"首页";
    homeVC.tabBarItem.image = [UIImage imageNamed:@"home02"];
    homeVC.tabBarItem.selectedImage = [UIImage imageNamed:@"home01"];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    
    strategyViewController *strategyVC = [[strategyViewController alloc] init];
    strategyVC.view.backgroundColor = [UIColor orangeColor];
    secondViewController *secondVC = [[secondViewController alloc] init];
    secondVC.view.backgroundColor = [UIColor blueColor];
    thirdViewController *thirdVC = [[thirdViewController alloc] init];
    thirdVC.view.backgroundColor = [UIColor redColor];
    fourthViewController *fourthVC = [[fourthViewController alloc] init];
    fourthVC.view.backgroundColor = [UIColor yellowColor];
    LPSwipeNavigationController * lopVC = [[LPSwipeNavigationController alloc]initWithViewControllers:@[strategyVC,secondVC,thirdVC,fourthVC] items:@[@"攻略",@"新口子",@"资讯",@"信用卡"]];
    lopVC.tabBarItem.title = @"攻略";
    lopVC.tabBarItem.image = [UIImage imageNamed:@"Raiders"];
    lopVC.tabBarItem.selectedImage = [UIImage imageNamed:@"raiders-click"];

    ZFBilViewController *bilVC = [ZFBilViewController new];
    bilVC.tabBarItem.title = @"贷款超市";
    bilVC.tabBarItem.image = [UIImage imageNamed:@"loansupermaeket"];
    bilVC.tabBarItem.selectedImage = [UIImage imageNamed:@"loansupermaekket-click"];
    UINavigationController *bilNav = [[UINavigationController alloc] initWithRootViewController:bilVC];
    
    ZFMyViewController *myVC = [ZFMyViewController new];
    myVC.tabBarItem.title = @"我的";
    myVC.tabBarItem.image = [UIImage imageNamed:@"my02"];
    myVC.tabBarItem.selectedImage = [UIImage imageNamed:@"my01"];
    UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController:myVC];
    
    ZFTabbarViewController *tabbarController = [ZFTabbarViewController new];
    tabbarController.viewControllers = @[homeNav,bilNav,lopVC,myNav];
    self.window.rootViewController = tabbarController;
}
- (void)setTabbarController1
{
    [self setBarAppearance];
    
    ZFHomeViewController *homeVC = [ZFHomeViewController new];
    homeVC.tabBarItem.title = @"首页";
    homeVC.tabBarItem.image = [UIImage imageNamed:@"home02"];
    homeVC.tabBarItem.selectedImage = [UIImage imageNamed:@"home01"];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    
    ZFMessageViewController *messageVC = [ZFMessageViewController new];
    messageVC.tabBarItem.title = @"消息";
    messageVC.tabBarItem.image = [UIImage imageNamed:@"bill02"];
    messageVC.tabBarItem.selectedImage = [UIImage imageNamed:@"bill01"];
    UINavigationController *messageNav = [[UINavigationController alloc] initWithRootViewController:messageVC];
    
    ZFAppleViewController *appleVC = [ZFAppleViewController new];
    appleVC.tabBarItem.title = @"贷款超市";
    appleVC.tabBarItem.image = [UIImage imageNamed:@"news01"];
    appleVC.tabBarItem.selectedImage = [UIImage imageNamed:@"news02"];
    UINavigationController *bilNav = [[UINavigationController alloc] initWithRootViewController:appleVC];
    
    ZFMyViewController *myVC = [ZFMyViewController new];
    myVC.tabBarItem.title = @"我的";
    myVC.tabBarItem.image = [UIImage imageNamed:@"my02"];
    myVC.tabBarItem.selectedImage = [UIImage imageNamed:@"my01"];
    UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController:myVC];
    
    ZFTabbarViewController *tabbarController = [ZFTabbarViewController new];
    tabbarController.viewControllers = @[homeNav,messageNav,bilNav,myNav];
    self.window.rootViewController = tabbarController;
}

#pragma mark ———— 设置导航栏、底部栏的外观
- (void)setBarAppearance
{
    /**
     *  设置导航栏外观
     */
    [UINavigationBar appearance].barTintColor = Main_Color1;//导航栏颜色
    [UINavigationBar appearance].backgroundColor = Main_Color1;

    [UINavigationBar appearance].titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:20]};//导航栏标题颜色，大小
    [UINavigationBar appearance].tintColor = [UIColor whiteColor];//返回键颜色
    [UINavigationBar appearance].backIndicatorTransitionMaskImage = [UIImage imageNamed:@"返回键"];
    [UINavigationBar appearance].backIndicatorImage = [UIImage imageNamed:@"返回键"];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

    /**
     *  tabbar的外观
     */
    [UITabBar appearance].barTintColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    [UITabBar appearance].tintColor = Main_Color1;
}

@end
